package com.zjc.stock.calculator;

import com.zjc.stock.StockExchange;
import com.zjc.stock.TransactionType;
import com.zjc.stock.model.Account;
import com.zjc.stock.model.FeeDetail;

import java.text.NumberFormat;

public class StockTransactionFeeCalculator {

    private final TransactionType type;
    private final double amount;    // 成交金额（元）
    private final double commissionRate;  // 佣金费率（默认0.0003）
    private final double minCommission;   // 最低佣金（默认5元）
    private final double stampTaxRate;    // 印花税率（默认0.0005）
    private final double transferFeeRate; // 过户费率（默认0.00001）

    public StockTransactionFeeCalculator(Account account, TransactionType type, double amount) {
        this(type, amount, account.getCommissionRate(), StockExchange.minCommission, StockExchange.stampTaxRate, account.getTransferFeeRate());
    }

    public StockTransactionFeeCalculator(TransactionType type, double amount, double commissionRate, double minCommission, double stampTaxRate, double transferFeeRate) {
        this.type = type;
        this.amount = amount;
        this.commissionRate = commissionRate;
        this.minCommission = minCommission;
        this.stampTaxRate = stampTaxRate;
        this.transferFeeRate = transferFeeRate;
    }

    // 计算佣金
    private double calculateCommission() {
        double commission = amount * commissionRate;
        return Math.max(commission, minCommission);
    }

    // 计算印花税（仅卖出时收取）
    private double calculateStampTax() {
        return (type == TransactionType.SELL) ? amount * stampTaxRate : 0.0;
    }

    // 计算过户费
    private double calculateTransferFee() {
        return amount * transferFeeRate;
    }

    // 计算总手续费
    public double calculateTotalFee() {
        return calculateCommission() + calculateStampTax() + calculateTransferFee();
    }

    public FeeDetail feeDetails() {
        FeeDetail feeDetail = new FeeDetail();
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        nf.setMaximumFractionDigits(2);

        feeDetail.setType(type);
        feeDetail.setAmount(amount);
        feeDetail.setCommission(calculateCommission());
        feeDetail.setStampTax(calculateStampTax());
        feeDetail.setTransferFee(calculateTransferFee());
        feeDetail.setTotalFee(calculateTotalFee());

        return feeDetail;
    }


}
